package com.system.exception;

import java.lang.reflect.Field;

import org.springframework.stereotype.Component;

@Component
public class ErrorCodeHelper {
	private final int defaultErrorCode = ErrorCode.SystemError.UNKNOW_ERROR;

	@SuppressWarnings("unchecked")
	public int getErrorCode(String catalog, String name) {
		int value = defaultErrorCode;
		Class[] classes = ErrorCode.class.getDeclaredClasses();
		for (Class clazz : classes) {
			String className = clazz.getName();
			int index = className.lastIndexOf("$");
			className = className.substring(index + 1);
			if (className.equals(catalog)) {
				try {
					Field field = clazz.getDeclaredField(name);
					value = (Integer) field.get(clazz);
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (NoSuchFieldException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		return value;
	}

	public static void main(String[] args) {
		ErrorCodeHelper helper = new ErrorCodeHelper();
		int result = helper.getErrorCode("CommonResult", "LOCK_SCCESS");
		System.out.println(result);
	}
}
